package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.WorkExperienceListAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.VitaDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WorkExperienceListActivity extends BaseActivity {

    String resumeId;

    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_education_experience_list, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("工作经历");
        resumeId = getIntent().getStringExtra("resumeId");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        educationExperienceListAdapter = new WorkExperienceListAdapter(this, new ArrayList<VitaDetailDataBean.WorkListBean>());
        recycleView.setAdapter(educationExperienceListAdapter);
        educationExperienceListAdapter.setOnClickCallBack(new WorkExperienceListAdapter.OnClickCallBack() {
            @Override
            public void deleCallBack(VitaDetailDataBean.WorkListBean item) {
                delWorkExperience(item.getId());
            }

            @Override
            public void editCallBack(VitaDetailDataBean.WorkListBean item) {
                Intent intent = new Intent(WorkExperienceListActivity.this, WorkExperienceActivity.class);
                intent.putExtra("WorkListBean", item);
                intent.putExtra("resumeId", resumeId);
                startActivity(intent);
            }
        });

        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();

        getData();
    }

    WorkExperienceListAdapter educationExperienceListAdapter;

    public void getData() {

        Map map = new HashMap();
        map.put("token", SPUtils.getToken());
        map.put("userId", SPUtils.getUserID());
        HttpMehtod.getInstance().getVita(map, new IdeaObserver<BaseDataBean<VitaDetailDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<VitaDetailDataBean> vitaDetailDataBeanBaseDataBean) {

                if (vitaDetailDataBeanBaseDataBean.getData() != null && vitaDetailDataBeanBaseDataBean.getData().getWork_list() != null) {
                    educationExperienceListAdapter.notifyDataSetChanged((ArrayList<VitaDetailDataBean.WorkListBean>) vitaDetailDataBeanBaseDataBean.getData().getWork_list());
                }

            }

            @Override
            public void onFail(int errorCode) {

            }


        });
    }


    private void delWorkExperience(String workId) {
        Map map = new HashMap();
        map.put("workId", workId);
        HttpMehtod.getInstance().delWorkExperience(map, new IdeaObserver<BaseDataBean>(this, true) {
            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
                ToastUtil.show("删除成功");
                getData();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

}
